/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.Warehouse;

import entity.Employee;
import entity.Inventory;
import entity.MaterialRequired;
import entity.MaterialRequirementPlan;
import entity.RawMaterial;
import entity.RawMaterialDelivery;
import entity.RawMaterialPurchaseOrder;
import entity.Supplier;
import entity.TrackingItem;
import entity.WarehouseZone;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import managedbean.UserSessionManagerBean;
import session.stateless.EmployeeSessionBeanLocal;
import session.stateless.SupplierSessionBeanLocal;
import session.stateless.ProductSessionBeanLocal;
import session.stateless.LogSessionBeanLocal;
import session.stateless.RawMaterialPurchaseOrderSessionBeanLocal;
import session.stateless.WarehouseSessionBeanLocal;

@ManagedBean(name = "rawMaterialPurchasingManagerBean")
@RequestScoped
public class RawMaterialPurchasingManagerBean implements Serializable {

    @EJB
    private SupplierSessionBeanLocal supplierSessionBeanLocal;
    @EJB
    private ProductSessionBeanLocal productSessionBeanLocal;
    @EJB
    private LogSessionBeanLocal logSessionBeanLocal;
    @EJB
    private RawMaterialPurchaseOrderSessionBeanLocal rawMaterialPurchaseOrderSessionBeanLocal;
    @EJB
    private WarehouseSessionBeanLocal warehouseSessionBeanLocal;
    @EJB
    private EmployeeSessionBeanLocal employeeSessionBeanLocal;
    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    private String loadViewMaterialRequirementPlans;
    private String loadAddRMPO;
    private String loadSearchRawMaterialPurchaseOrders;
    private String loadViewRMPO;
    private String loadReceiveRawMaterialPurchaseOrders;
    private String loadReceiveRMPO;
    private String loadSearchRawMaterialDeliveries;
    private String loadViewRMD;
    private RawMaterialPurchaseOrder selectedRawMaterialPurchaseOrder;
    private MaterialRequirementPlan[] selectedMaterialRequirementPlans;
    private Supplier selectedSupplier;
    private RawMaterialDelivery selectedRawMaterialDelivery;
    private List<Supplier> suppliers = new ArrayList<Supplier>();
    private Timestamp expiryDate;
    private Date todayTimestampPlus60;
    private Date todayTimestamp;
    //RawMaterialDelivery
    private Double amountReceived;
    private RawMaterial rawMaterial = new RawMaterial();
    private WarehouseZone selectedWarehouseZone;
    private String deliveryNumber;
    private Date receivedDate;
    //RawMaterialPurchasingOrder
    private Date deliveryDate;
    private String remark;
    private Employee employee;
    private Supplier supplier;
    private Double amountToPay;
    private List<MaterialRequirementPlan> materialRequirementPlans = new ArrayList<MaterialRequirementPlan>();
    private List<RawMaterialPurchaseOrder> rawMaterialPurchaseOrders;
    private List<RawMaterialPurchaseOrder> rawMaterialPurchaseOrdersToReceive = new ArrayList<RawMaterialPurchaseOrder>();
    private List<RawMaterial> rawMaterials = new ArrayList<RawMaterial>();
    private List<WarehouseZone> warehouseZones;
    private List<RawMaterialDelivery> rawMaterialDeliveries = new ArrayList<RawMaterialDelivery>();

    public void removeBatchFromWH(ActionEvent event) {
        try {
            RawMaterialDelivery rmd = selectedRawMaterialDelivery;

            MaterialRequired mr = rmd.getMaterialRequired();

            //deduct warehousezone current quantity
            WarehouseZone whz = rmd.getWarehouseZone();

            whz.setCurrentQuantity(whz.getCurrentQuantity() - mr.getWeight());

            warehouseSessionBeanLocal.updateWarehouseZone(whz);

            //dequeue from warehouse's raw material deliveries
            warehouseSessionBeanLocal.removeRMDFromWH(Long.parseLong("1000"), rmd.getId());

            //add inventory quantity
            Inventory i = warehouseSessionBeanLocal.getInventoryByRawMaterial(mr.getRawMaterial().getName());

            i.setQuantity(i.getQuantity() - mr.getWeight());

            warehouseSessionBeanLocal.updateInventory(i);

            //add inventory tracking item
            TrackingItem ti = new TrackingItem();
            ti.setDescription("-" + mr.getWeight() + "");

            warehouseSessionBeanLocal.updateInventory(i, ti);

            //update mr weight to 0
            mr.setWeight(0.0);

            warehouseSessionBeanLocal.updateMaterialRequired(mr);

            //redirect to success page
            ArrayList<String> al = new ArrayList<String>();
            al.add("Raw material batch removal");
            al.add("You have successfully removed a raw material batch.");
            al.add("Click on the link to to ");
            al.add("continue");
            al.add("Web_Warehouse/searchRawMaterialDeliveries.xhtml");
            userSessionManagerBean.setSelectedItem(al);

            logSessionBeanLocal.addLog("Remove raw material batch", "Raw material batch removed: " + userSessionManagerBean.getUserId(), "viewRawMaterialDelivery.xhtml", userSessionManagerBean.getUserId());

            FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                    + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort()
                    + "/MerlionERP-war/success.xhtml");
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void viewDelivery(ActionEvent event) {
        try {
            if (selectedRawMaterialDelivery != null) {
                userSessionManagerBean.setSelectedRawMaterialDelivery(selectedRawMaterialDelivery);

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/viewRawMaterialDelivery.xhtml"); //redirect to viewRawMaterialDelivery.xhtml  
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a raw material delivery record before continuing.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void recordDelivery(ActionEvent event) {
        try {
            if (selectedWarehouseZone != null) {
                if (rawMaterial.getId() != null) {
                    MaterialRequirementPlan temp = null;
                    for (MaterialRequirementPlan mrp : selectedRawMaterialPurchaseOrder.getMaterialRequirementPlans()) {
                        if (mrp.getRawMaterial().getId().compareTo(rawMaterial.getId()) == 0) {
                            temp = mrp;
                        }
                    }

                    if (temp.getWeight().compareTo(amountReceived) == 0) {
                        amountReceived = round(amountReceived, 2);

                        RawMaterial rm = productSessionBeanLocal.getRawMaterial(rawMaterial.getId());

                        MaterialRequired mr = new MaterialRequired(amountReceived, rm);

                        Employee e = employeeSessionBeanLocal.getEmployee(userSessionManagerBean.getUserId());

                        Supplier s = selectedRawMaterialPurchaseOrder.getSupplier();

                        Timestamp ts = new Timestamp(receivedDate.getTime());

                        Date temp2 = receivedDate;

                        Calendar cal = Calendar.getInstance();

                        cal.setTime(temp2);

                        cal.add(Calendar.MONTH, rm.getShelfLife());

                        expiryDate = new Timestamp(cal.getTime().getTime());

                        RawMaterialDelivery rmd = new RawMaterialDelivery(e, s, selectedWarehouseZone, deliveryNumber, ts, selectedRawMaterialPurchaseOrder, mr, expiryDate);

                        //add warehousezone current quantity
                        selectedWarehouseZone.setCurrentQuantity(selectedWarehouseZone.getCurrentQuantity() + amountReceived);

                        warehouseSessionBeanLocal.updateWarehouseZone(selectedWarehouseZone);

                        //enqueue to warehouse's raw material deliveries
                        warehouseSessionBeanLocal.updateWarehouse(Long.parseLong("1000"), rmd);

                        //add inventory quantity
                        Inventory i = warehouseSessionBeanLocal.getInventoryByRawMaterial(rm.getName());

                        i.setQuantity(i.getQuantity() + amountReceived);

                        warehouseSessionBeanLocal.updateInventory(i);

                        //add inventory tracking item
                        TrackingItem ti = new TrackingItem();
                        ti.setDescription("+" + amountReceived + "");

                        warehouseSessionBeanLocal.updateInventory(i, ti);

                        FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                                + getUserSessionManagerBean().getServerName() + ":"
                                + getUserSessionManagerBean().getServerPort()
                                + "/MerlionERP-war/Web_Warehouse/receiveRawMaterialPurchaseOrder.xhtml"); //redirect to receiveRawMaterialPurchaseOrder.xhtml  
                    } else {
                        FacesContext.getCurrentInstance().addMessage(null,
                                new FacesMessage(FacesMessage.SEVERITY_ERROR, "Amount received must be equal to the corresponding material requirement plan's amount.", ""));
                    }
                } else {
                    FacesContext.getCurrentInstance().addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select the raw material that was delivered before continuing.", ""));
                }
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a zone to store the raw material before continuing.", ""));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void receiveRMPO(ActionEvent event) {
        try {
            if (selectedRawMaterialPurchaseOrder != null) {
                userSessionManagerBean.setSelectedRawMaterialPurchaseOrder(selectedRawMaterialPurchaseOrder);

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/receiveRawMaterialPurchaseOrder.xhtml"); //redirect to receiveRawMaterialPurchaseOrder.xhtml   
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a raw material purchase order before continuing.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void makePayment(ActionEvent event) {
        try {
            if (selectedRawMaterialPurchaseOrder != null) {
                if (!selectedRawMaterialPurchaseOrder.getStatus().equals("Paid")) {
                    userSessionManagerBean.setSelectedRawMaterialPurchaseOrder(selectedRawMaterialPurchaseOrder);

                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                            + getUserSessionManagerBean().getServerName() + ":"
                            + getUserSessionManagerBean().getServerPort()
                            + "/MerlionERP-war/Web_Finance/payForRMPO.xhtml"); //redirect to payForRMPO.xhtml    
                } else {
                    FacesContext.getCurrentInstance().addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "The selected raw material purchase order has already been paid for.", ""));
                }
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a raw material purchase order before continuing.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void back(ActionEvent event) {
        try {
            if (event.getComponent().getId().equals("btnBackFromAddRMPO")) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/viewMaterialRequirementPlans.xhtml"); //redirect to viewMaterialRequirementPlans.xhtml
            } else if (event.getComponent().getId().equals("btnBackFromViewRMPO")) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/searchRawMaterialPurchaseOrders.xhtml"); //redirect to viewMaterialRequirementPlans.xhtml
            } else if (event.getComponent().getId().equals("btnDone")) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/receiveRawMaterialPurchaseOrders.xhtml"); //redirect to receiveRawMaterialPurchaseOrders.xhtml
            } else if (event.getComponent().getId().equals("btnBackFromViewRMD")) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/searchRawMaterialDeliveries.xhtml"); //redirect to searchRawMaterialDeliveries.xhtml
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void viewOrder(ActionEvent event) {
        try {
            if (selectedRawMaterialPurchaseOrder != null) {
                userSessionManagerBean.setSelectedRawMaterialPurchaseOrder(selectedRawMaterialPurchaseOrder);

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/viewRawMaterialPurchaseOrder.xhtml");
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a supplier before continuing.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void placeAnOrder(ActionEvent event) {
        try {
            if (selectedSupplier != null) {
                Timestamp ts = new Timestamp(deliveryDate.getTime());

                amountToPay = round(amountToPay, 2);

                for (MaterialRequirementPlan mrp : materialRequirementPlans) {
                    Double weight = mrp.getWeight();
                    Integer lotSize = mrp.getRawMaterial().getLotSize();
                    Double lots = 0.0;

                    lots = weight / lotSize;
                    lots = Math.ceil(lots);

                    weight = lots * lotSize;

                    mrp.setWeight(weight);
                    mrp.setStatus("Ordered");

                    rawMaterialPurchaseOrderSessionBeanLocal.updateMRP(mrp);
                }

                List<MaterialRequirementPlan> temp = new ArrayList<MaterialRequirementPlan>();
                for (MaterialRequirementPlan mrp : materialRequirementPlans) {
                    MaterialRequirementPlan temp2 = rawMaterialPurchaseOrderSessionBeanLocal.getMRP(mrp.getId());

                    temp.add(temp2);
                }

                rawMaterialPurchaseOrderSessionBeanLocal.addRawMaterialPO(ts, remark, userSessionManagerBean.getUserId(), selectedSupplier, temp, amountToPay);

                //redirect to success page
                ArrayList<String> al = new ArrayList<String>();
                al.add("Raw material purchasing order");
                al.add("You have successfully made a raw material purchasing order.");
                al.add("Click on the link to to ");
                al.add("continue");
                al.add("Web_Warehouse/searchRawMaterialPurchaseOrders.xhtml");
                userSessionManagerBean.setSelectedItem(al);

                logSessionBeanLocal.addLog("Add raw material purchasing order", "Raw material purchasing order created: " + userSessionManagerBean.getUserId(), "createRawMaterialPurchaseOrder.xhtml", userSessionManagerBean.getUserId());

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort()
                        + "/MerlionERP-war/success.xhtml");
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a supplier before continuing.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void placeOrder(ActionEvent event) {
        try {
            if (selectedMaterialRequirementPlans.length > 0) {
                userSessionManagerBean.setSelectedMaterialRequirementPlans(selectedMaterialRequirementPlans);

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/createRawMaterialPurchaseOrder.xhtml");
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select at least a material requirement plan before continuing.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Getter and Setter. Click on the + sign on the left to edit the code.">  
    /**
     * @return the supplierSessionBeanLocal
     */
    public SupplierSessionBeanLocal getSupplierSessionBeanLocal() {
        return supplierSessionBeanLocal;
    }

    /**
     * @param supplierSessionBeanLocal the supplierSessionBeanLocal to set
     */
    public void setSupplierSessionBeanLocal(SupplierSessionBeanLocal supplierSessionBeanLocal) {
        this.supplierSessionBeanLocal = supplierSessionBeanLocal;
    }

    /**
     * @return the productSessionBeanLocal
     */
    public ProductSessionBeanLocal getProductSessionBeanLocal() {
        return productSessionBeanLocal;
    }

    /**
     * @param productSessionBeanLocal the productSessionBeanLocal to set
     */
    public void setProductSessionBeanLocal(ProductSessionBeanLocal productSessionBeanLocal) {
        this.productSessionBeanLocal = productSessionBeanLocal;
    }

    /**
     * @return the logSessionBeanLocal
     */
    public LogSessionBeanLocal getLogSessionBeanLocal() {
        return logSessionBeanLocal;
    }

    /**
     * @param logSessionBeanLocal the logSessionBeanLocal to set
     */
    public void setLogSessionBeanLocal(LogSessionBeanLocal logSessionBeanLocal) {
        this.logSessionBeanLocal = logSessionBeanLocal;
    }

    /**
     * @return the rawMaterialPurchasingSessionBeanLocal
     */
    public RawMaterialPurchaseOrderSessionBeanLocal getRawMaterialPurchaseOrderSessionBeanLocal() {
        return rawMaterialPurchaseOrderSessionBeanLocal;
    }

    /**
     * @param rawMaterialPurchasingSessionBeanLocal the rawMaterialPurchasingSessionBeanLocal to set
     */
    public void setRawMaterialPurchaseOrderSessionBeanLocal(RawMaterialPurchaseOrderSessionBeanLocal rawMaterialPurchaseOrderSessionBeanLocal) {
        this.rawMaterialPurchaseOrderSessionBeanLocal = rawMaterialPurchaseOrderSessionBeanLocal;
    }

    /**
     * @return the userSessionManagerBean
     */
    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    /**
     * @param userSessionManagerBean the userSessionManagerBean to set
     */
    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    /**
     * @return the loadViewMaterialRequirementPlans
     */
    public String getLoadViewMaterialRequirementPlans() {
        List<MaterialRequirementPlan> temp = rawMaterialPurchaseOrderSessionBeanLocal.getMaterialRequirementPlans();

        for (MaterialRequirementPlan mrp : temp) {
            if (mrp.getStatus().equals("Pending")) {
                materialRequirementPlans.add(mrp);
            }
        }

        return null;
    }

    /**
     * @param loadViewMaterialRequirementPlans the loadViewMaterialRequirementPlans to set
     */
    public void setLoadViewMaterialRequirementPlans(String loadManageMaterialRequirementPlans) {
        this.loadViewMaterialRequirementPlans = loadManageMaterialRequirementPlans;
    }

    /**
     * @return the deliveryDate
     */
    public Date getDeliveryDate() {
        return deliveryDate;
    }

    /**
     * @param deliveryDate the deliveryDate to set
     */
    public void setDeliveryDate(Date deliveryDate) {
        this.deliveryDate = deliveryDate;
    }

    /**
     * @return the remark
     */
    public String getRemark() {
        return remark;
    }

    /**
     * @param remark the remark to set
     */
    public void setRemark(String remark) {
        this.remark = remark;
    }

    /**
     * @return the employee
     */
    public Employee getEmployee() {
        return employee;
    }

    /**
     * @param employee the employee to set
     */
    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    /**
     * @return the supplier
     */
    public Supplier getSupplier() {
        return supplier;
    }

    /**
     * @param supplier the supplier to set
     */
    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    /**
     * @return the materialRequirementPlans
     */
    public List<MaterialRequirementPlan> getMaterialRequirementPlans() {
        return materialRequirementPlans;
    }

    /**
     * @param materialRequirementPlans the materialRequirementPlans to set
     */
    public void setMaterialRequirementPlans(List<MaterialRequirementPlan> materialRequirementPlans) {
        this.materialRequirementPlans = materialRequirementPlans;
    }
    //</editor-fold>

    /**
     * @return the loadAddRMPO
     */
    public String getLoadAddRMPO() {
        selectedMaterialRequirementPlans = userSessionManagerBean.getSelectedMaterialRequirementPlans();

        materialRequirementPlans = new ArrayList<MaterialRequirementPlan>();
        for (Integer i = 0; i < selectedMaterialRequirementPlans.length; i++) {
            materialRequirementPlans.add(selectedMaterialRequirementPlans[i]);
        }

        List<Supplier> temp = supplierSessionBeanLocal.getSuppliers();

        for (Supplier s : temp) {
            List<RawMaterial> temp2 = s.getRawMaterials();

            Boolean has = false;
            for (MaterialRequirementPlan mrp : materialRequirementPlans) {
                for (RawMaterial rm : temp2) {
                    if (rm.getId().compareTo(mrp.getRawMaterial().getId()) == 0) {
                        has = true;
                    }
                }
            }

            if (has) {
                suppliers.add(s);
            }
        }

        return null;
    }

    public double round(double valueToRound, int numberOfDecimalPlaces) {
        double multiplicationFactor = Math.pow(10, numberOfDecimalPlaces);
        double interestedInZeroDPs = valueToRound * multiplicationFactor;

        return Math.round(interestedInZeroDPs) / multiplicationFactor;
    }

    /**
     * @param loadAddRMPO the loadAddRMPO to set
     */
    public void setLoadAddRMPO(String loadAddRMPO) {
        this.loadAddRMPO = loadAddRMPO;
    }

    /**
     * @return the selectedSupplier
     */
    public Supplier getSelectedSupplier() {
        return selectedSupplier;
    }

    /**
     * @param selectedSupplier the selectedSupplier to set
     */
    public void setSelectedSupplier(Supplier selectedSupplier) {
        this.selectedSupplier = selectedSupplier;
    }

    /**
     * @return the suppliers
     */
    public List<Supplier> getSuppliers() {
        return suppliers;
    }

    /**
     * @param suppliers the suppliers to set
     */
    public void setSuppliers(List<Supplier> suppliers) {
        this.suppliers = suppliers;
    }

    /**
     * @return the selectedMaterialRequirementPlan
     */
    public MaterialRequirementPlan[] getSelectedMaterialRequirementPlans() {
        return selectedMaterialRequirementPlans;
    }

    /**
     * @param selectedMaterialRequirementPlan the selectedMaterialRequirementPlan to set
     */
    public void setSelectedMaterialRequirementPlans(MaterialRequirementPlan[] selectedMaterialRequirementPlans) {
        this.selectedMaterialRequirementPlans = selectedMaterialRequirementPlans;
    }

    /**
     * @return the loadSearchRawMaterialPurchaseOrders
     */
    public String getLoadSearchRawMaterialPurchaseOrders() {
        rawMaterialPurchaseOrders = rawMaterialPurchaseOrderSessionBeanLocal.getRawMaterialPOs();

        return null;
    }

    /**
     * @param loadSearchRawMaterialPurchaseOrders the loadSearchRawMaterialPurchaseOrders to set
     */
    public void setLoadSearchRawMaterialPurchaseOrders(String loadSearchRawMaterialPurchaseOrders) {
        this.loadSearchRawMaterialPurchaseOrders = loadSearchRawMaterialPurchaseOrders;
    }

    /**
     * @return the rawMaterialPurchaseOrders
     */
    public List<RawMaterialPurchaseOrder> getRawMaterialPurchaseOrders() {
        return rawMaterialPurchaseOrders;
    }

    /**
     * @param rawMaterialPurchaseOrders the rawMaterialPurchaseOrders to set
     */
    public void setRawMaterialPurchaseOrders(List<RawMaterialPurchaseOrder> rawMaterialPurchaseOrders) {
        this.rawMaterialPurchaseOrders = rawMaterialPurchaseOrders;
    }

    /**
     * @return the selectedRawMaterialPurchaseOrder
     */
    public RawMaterialPurchaseOrder getSelectedRawMaterialPurchaseOrder() {
        return selectedRawMaterialPurchaseOrder;
    }

    /**
     * @param selectedRawMaterialPurchaseOrder the selectedRawMaterialPurchaseOrder to set
     */
    public void setSelectedRawMaterialPurchaseOrder(RawMaterialPurchaseOrder selectedRawMaterialPurchaseOrder) {
        this.selectedRawMaterialPurchaseOrder = selectedRawMaterialPurchaseOrder;
    }

    /**
     * @return the amountToPay
     */
    public Double getAmountToPay() {
        return amountToPay;
    }

    /**
     * @param amountToPay the amountToPay to set
     */
    public void setAmountToPay(Double amountToPay) {
        this.amountToPay = amountToPay;
    }

    /**
     * @return the loadViewRMPO
     */
    public String getLoadViewRMPO() {
        selectedRawMaterialPurchaseOrder = userSessionManagerBean.getSelectedRawMaterialPurchaseOrder();

        return null;
    }

    /**
     * @param loadViewRMPO the loadViewRMPO to set
     */
    public void setLoadViewRMPO(String loadViewRMPO) {
        this.loadViewRMPO = loadViewRMPO;
    }

    /**
     * @return the rawMaterialPurchaseOrdersToReceive
     */
    public List<RawMaterialPurchaseOrder> getRawMaterialPurchaseOrdersToReceive() {
        return rawMaterialPurchaseOrdersToReceive;
    }

    /**
     * @param rawMaterialPurchaseOrdersToReceive the rawMaterialPurchaseOrdersToReceive to set
     */
    public void setRawMaterialPurchaseOrdersToReceive(List<RawMaterialPurchaseOrder> rawMaterialPurchaseOrdersToReceive) {
        this.rawMaterialPurchaseOrdersToReceive = rawMaterialPurchaseOrdersToReceive;
    }

    /**
     * @return the loadReceiveRawMaterialPurchaseOrders
     */
    public String getLoadReceiveRawMaterialPurchaseOrders() {
        List<RawMaterialPurchaseOrder> temp = rawMaterialPurchaseOrderSessionBeanLocal.getRMPOsByStatus("Paid");

        for (RawMaterialPurchaseOrder rmpo : temp) {
            List<RawMaterialDelivery> temp2 = rawMaterialPurchaseOrderSessionBeanLocal.getRMDsByRMPO(rmpo.getId());

            if (temp2.size() < rmpo.getMaterialRequirementPlans().size()) {
                rawMaterialPurchaseOrdersToReceive.add(rmpo);
            }
        }

        return null;
    }

    /**
     * @param loadReceiveRawMaterialPurchaseOrders the loadReceiveRawMaterialPurchaseOrders to set
     */
    public void setLoadReceiveRawMaterialPurchaseOrders(String loadReceiveRawMaterialPurchaseOrders) {
        this.loadReceiveRawMaterialPurchaseOrders = loadReceiveRawMaterialPurchaseOrders;
    }

    /**
     * @return the rawMaterial
     */
    public RawMaterial getRawMaterial() {
        return rawMaterial;
    }

    /**
     * @param rawMaterial the rawMaterial to set
     */
    public void setRawMaterial(RawMaterial rawMaterial) {
        this.rawMaterial = rawMaterial;
    }

    /**
     * @return the amountReceived
     */
    public Double getAmountReceived() {
        return amountReceived;
    }

    /**
     * @param amountReceived the amountReceived to set
     */
    public void setAmountReceived(Double amountReceived) {
        this.amountReceived = amountReceived;
    }

    /**
     * @return the whZone
     */
    public WarehouseZone getSelectedWarehouseZone() {
        return selectedWarehouseZone;
    }

    /**
     * @param whZone the whZone to set
     */
    public void setSelectedWarehouseZone(WarehouseZone selectedWarehouseZone) {
        this.selectedWarehouseZone = selectedWarehouseZone;
    }

    /**
     * @return the deliveryNumber
     */
    public String getDeliveryNumber() {
        return deliveryNumber;
    }

    /**
     * @param deliveryNumber the deliveryNumber to set
     */
    public void setDeliveryNumber(String deliveryNumber) {
        this.deliveryNumber = deliveryNumber;
    }

    /**
     * @return the receivedDate
     */
    public Date getReceivedDate() {
        return receivedDate;
    }

    /**
     * @param receivedDate the receivedDate to set
     */
    public void setReceivedDate(Date receivedDate) {
        this.receivedDate = receivedDate;
    }

    /**
     * @return the rawMaterials
     */
    public List<RawMaterial> getRawMaterials() {
        return rawMaterials;
    }

    /**
     * @param rawMaterials the rawMaterials to set
     */
    public void setRawMaterials(List<RawMaterial> rawMaterials) {
        this.rawMaterials = rawMaterials;
    }

    /**
     * @return the loadReceiveRMPO
     */
    public String getLoadReceiveRMPO() {
        selectedRawMaterialPurchaseOrder = userSessionManagerBean.getSelectedRawMaterialPurchaseOrder();

        warehouseZones = warehouseSessionBeanLocal.getWarehouseZones();

        rawMaterialDeliveries = rawMaterialPurchaseOrderSessionBeanLocal.getRMDsByRMPO(selectedRawMaterialPurchaseOrder.getId());

        for (MaterialRequirementPlan mrp : selectedRawMaterialPurchaseOrder.getMaterialRequirementPlans()) {
            if (rawMaterialDeliveries.size() > 0) {
                if (rawMaterialDeliveries.size() < selectedRawMaterialPurchaseOrder.getMaterialRequirementPlans().size()) {
                    for (RawMaterialDelivery rmd : rawMaterialDeliveries) {
                        if (rmd.getMaterialRequired().getRawMaterial().getId().compareTo(mrp.getRawMaterial().getId()) != 0) {
                            RawMaterial rm = productSessionBeanLocal.getRawMaterial(mrp.getRawMaterial().getId());

                            rawMaterials.add(rm);
                        }
                    }
                }
            } else {
                RawMaterial rm = productSessionBeanLocal.getRawMaterial(mrp.getRawMaterial().getId());

                rawMaterials.add(rm);
            }
        }

        return null;
    }

    /**
     * @param loadReceiveRMPO the loadReceiveRMPO to set
     */
    public void setLoadReceiveRMPO(String loadReceiveRMPO) {
        this.loadReceiveRMPO = loadReceiveRMPO;
    }

    /**
     * @return the warehouseZones
     */
    public List<WarehouseZone> getWarehouseZones() {
        return warehouseZones;
    }

    /**
     * @param warehouseZones the warehouseZones to set
     */
    public void setWarehouseZones(List<WarehouseZone> warehouseZones) {
        this.warehouseZones = warehouseZones;
    }

    /**
     * @return the rawMaterialDeliveries
     */
    public List<RawMaterialDelivery> getRawMaterialDeliveries() {
        return rawMaterialDeliveries;
    }

    /**
     * @param rawMaterialDeliveries the rawMaterialDeliveries to set
     */
    public void setRawMaterialDeliveries(List<RawMaterialDelivery> rawMaterialDeliveries) {
        this.rawMaterialDeliveries = rawMaterialDeliveries;
    }

    /**
     * @return the selectedRawMaterialDelivery
     */
    public RawMaterialDelivery getSelectedRawMaterialDelivery() {
        return selectedRawMaterialDelivery;
    }

    /**
     * @param selectedRawMaterialDelivery the selectedRawMaterialDelivery to set
     */
    public void setSelectedRawMaterialDelivery(RawMaterialDelivery selectedRawMaterialDelivery) {
        this.selectedRawMaterialDelivery = selectedRawMaterialDelivery;
    }

    /**
     * @return the loadSearchRawMaterialDeliveries
     */
    public String getLoadSearchRawMaterialDeliveries() {
        List<RawMaterialDelivery> temp = rawMaterialPurchaseOrderSessionBeanLocal.getRMDs();

        for (RawMaterialDelivery rmd : temp) {
            if (rmd.getMaterialRequired().getWeight() > 0) {
                rawMaterialDeliveries.add(rmd);
            }
        }

        return null;
    }

    /**
     * @param loadSearchRawMaterialDeliveries the loadSearchRawMaterialDeliveries to set
     */
    public void setLoadSearchRawMaterialDeliveries(String loadSearchRawMaterialDeliveries) {
        this.loadSearchRawMaterialDeliveries = loadSearchRawMaterialDeliveries;
    }

    /**
     * @return the loadViewRMD
     */
    public String getLoadViewRMD() {
        selectedRawMaterialDelivery = userSessionManagerBean.getSelectedRawMaterialDelivery();

        return null;
    }

    /**
     * @param loadViewRMD the loadViewRMD to set
     */
    public void setLoadViewRMD(String loadViewRMD) {
        this.loadViewRMD = loadViewRMD;
    }

    /**
     * @return the expiryDate
     */
    public Timestamp getExpiryDate() {
        return expiryDate;
    }

    /**
     * @param expiryDate the expiryDate to set
     */
    public void setExpiryDate(Timestamp expiryDate) {
        this.expiryDate = expiryDate;
    }

    /**
     * @return the todayTimestampMinus30
     */
    public Date getTodayTimestampPlus60() {
        Calendar cal = Calendar.getInstance();

        cal.add(cal.DAY_OF_YEAR, 60);

        Date d = cal.getTime();

        return d;
    }

    /**
     * @param todayTimestampMinus30 the todayTimestampMinus30 to set
     */
    public void setTodayTimestampMinus60(Date todayTimestampPlus60) {
        this.todayTimestampPlus60 = todayTimestampPlus60;
    }

    /**
     * @return the todayTimestampMinus60
     */
    public Date getTodayTimestamp() {
        Calendar cal = Calendar.getInstance();

        Date d = cal.getTime();

        return d;
    }

    /**
     * @param todayTimestampMinus60 the todayTimestampMinus60 to set
     */
    public void setTodayTimestamp(Date todayTimestamp) {
        this.todayTimestamp = todayTimestamp;
    }
}
